package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.juneng.common.Condition;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.BookType;
import com.juneng.mapper.BookTypeMapper;
import com.juneng.service.IBookTypeService;
import com.juneng.dto.BookTypeDto;
import com.juneng.dto.BookTypeQueryDto;
import com.juneng.vo.BookTypeChildrenVo;
import com.juneng.vo.BookTypeVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import java.util.List;
/**
 *  服务实现类
 *
 * @author zds
 * @since 2021-07-14
 */
@Service
@AllArgsConstructor
public class BookTypeServiceImpl implements IBookTypeService {

    protected BookTypeMapper bookTypeMapper;
    @Override
    public IPage<BookTypeVo> fatherPage(BookTypeQueryDto dto) {
        IPage<BookTypeVo> page = Condition.getPage(dto);
        if (dto.getBookTypeId() != null){
            return bookTypeMapper.getPage(page, dto);
        }
        return bookTypeMapper.page(page,dto);
    }
    //nextLevel
    @Override
    public List<BookType> list(BookTypeQueryDto dto) {
        QueryWrapper<BookType> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto,BookType.class));
        return bookTypeMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(BookTypeDto dto) {
        dto.setBookLevel(1);
        return bookTypeMapper.insert(BeanCopyUtils.copy(dto,BookType.class));
    }

    @Override
    public Integer updateById(BookTypeDto dto) {
        return bookTypeMapper.updateById(BeanCopyUtils.copy(dto,BookType.class));
    }

    @Override
    public Integer deleteLogic(List<Integer> toIntList) {
        return bookTypeMapper.deleteBatchIds(toIntList);
    }

    @Override
    public BookType getById(Integer id) {
        return bookTypeMapper.selectById(id);
    }

    @Override
    public IPage<BookTypeVo> sonPage(BookTypeQueryDto dto) {
        IPage<BookTypeVo> page = Condition.getPage(dto);
        return bookTypeMapper.sonPage(page,dto);
    }

    @Override
    public Integer saveParent(BookTypeDto dto) {
        dto.setBookLevel(0);
        return bookTypeMapper.insert(BeanCopyUtils.copy(dto,BookType.class));
    }

    @Override
    public List<BookTypeChildrenVo> children() {
        return bookTypeMapper.selectChildren();
    }

    @Override
    public List<BookType> sonList(BookTypeQueryDto dto) {
        QueryWrapper<BookType> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto,BookType.class));
        queryWrapper.eq("book_level",0);
        return bookTypeMapper.selectList(queryWrapper);
    }
}
